import { ColorConfig } from "../../engine/ColorConfig";
import DateUtil from "../../engine/DateUtil";
import { Logger } from "../../engine/Logger";
import UIManager from "../../engine/UIManager";
import GameController from "../../GameController";
import { AudioHelper } from "../audio/AudioHelper";
import { ViewConfig } from "../config/ViewConfig";
import { ViewCheck } from "../view/ViewCheck";

export class UIHelper {

    private static lastClickTime: number = 0

    public static onAddToggleContainerEvent(toggleContainer: cc.ToggleContainer, node: cc.Node, component: string, handler: string) {
        var eventHandler: cc.Component.EventHandler = new cc.Component.EventHandler();
        eventHandler.target = node; //这个 node 节点是你的事件处理代码组件所属的节点
        eventHandler.component = component;
        eventHandler.handler = handler;
        toggleContainer.checkEvents[0] = eventHandler;
    }

    public static setColor(node: cc.Node, colorHex: string = "#00FF80") {
        node.color = ColorConfig.hexToColor(colorHex)
        node.children.forEach(childNode => {
            this.setColor(childNode, colorHex)
        });
    }

    public static onAddToggleEvent() {

    }

    // public static onAddEvent(btnDisplay: cc.Node, callback: Function, transition: number = cc.Button.Transition.SCALE, zoneScale: number = 1.05) {
    public static onAddEvent(btnDisplay: cc.Node, callback: Function, transition: number = cc.Button.Transition.SCALE, zoneScale: number = 1.1) {
        let btn: cc.Button = btnDisplay.getComponent(cc.Button)
        if (!btn) {
            btn = btnDisplay.addComponent(cc.Button);
            btn.transition = transition;
            btn.zoomScale = zoneScale
        }
        btnDisplay.targetOff(btnDisplay);
        btnDisplay.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
            if (btn.enabled) {
                if (DateUtil.now() - this.lastClickTime >= 100) {
                    this.lastClickTime = DateUtil.now()
                    GameController.instance.onCLickEvent(event)
                    //加点击音效
                    AudioHelper.playPress()
                    callback && callback();
                }
            }
        }, btnDisplay);
    }

    public static hadChinaWord(nowStr: string): boolean {
        for (let index = 0; index < nowStr.length; index++) {
            let charCode: number = nowStr.charCodeAt(index);
            if (charCode >= 0x4E00 || charCode <= 0x9FFF) {
                return true;
            }
        }
        return false;
    }

    public static playOpacityAppear(node: cc.Node, callback: Function = null) {
        node.opacity = 0;
        cc.tween(node).to(2, { opacity: 255 }, {
            easing: "expoInOut",       // 缓动函数，可以使用已有的，也可以传入自定义的函数。      
        }).call(() => {
            callback && callback();
        }).start()
    }

    public static showTip(tips: string) {
        UIManager.instance.showView(ViewConfig.viewConfig.ViewTip, null, tips);
    }

    public static showCheck(tips: string, callback: Function = null, dialogType: number = ViewCheck.Dialog_Type.OK) {
        UIManager.instance.showView(ViewConfig.viewConfig.ViewCheck, null, { "tips": tips, "dialogType": dialogType, "callback": callback })
    }

    public static formatCoin(coin: number) {
        let str: string = ""
        if (coin >= 10000) {
            str = (coin / 10000).toFixed(2) + "万"
        } else {
            str = "" + coin
        }
        return str
    }



}